package com.example.studyPlatform.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import static com.example.studyPlatform.controller.api.kit.JsonUtils.toMap;

@Component
public class ViewResolverConfig implements WebMvcConfigurer {
    ThymeleafViewResolver thymeleafViewResolver;


    public ViewResolverConfig(@Qualifier("thymeleafViewResolver") ThymeleafViewResolver thymeleafViewResolver) {
        this.thymeleafViewResolver = thymeleafViewResolver;
    }

    @Override
    // 添加全局变量
    public void configureViewResolvers(ViewResolverRegistry registry) {
        if (thymeleafViewResolver != null) {
            thymeleafViewResolver.setStaticVariables(toMap(new BaseConstant()));
            thymeleafViewResolver.setStaticVariables(toMap(new RoutingConstant()));
        }
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedMethods("*")
//                .allowedOrigins("http://127.0.0.1:9876")
//                .allowedHeaders("*");
//        WebMvcConfigurer.super.addCorsMappings(registry);
    }
}

